package com.example.demo.demo.collection;


import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

//import static com.sun.xml.internal.fastinfoset.util.ValueArray.MAXIMUM_CAPACITY;

/**
 * @author: htc
 * @date: Created in 10:47 2018/9/27.
 */
public class HashMapTest {
    public static void main(String[] args) {
        int cap = 10;
        int n = cap - 1;
        n |= n >>> 1;
        n |= n >>> 2;
        n |= n >>> 4;
        n |= n >>> 8;
        n |= n >>> 16;
        System.out.println(n + 1);

        test1();

//        swap();
        hash();
        add();
        tableSizeFor();
        keySet();
    }


    public static void test1() {
        System.out.println("start !!!");
        HashMap map = new HashMap();

        //测试HashMap 的扩容机制
        for (int i = 0; i < 12; i++) {
            map.put(i,i);
        }
        map.put("a", 1);
        map.put("b", 2);
        map.put("c", "a");
        Set keys = map.keySet();
        Collection value = map.values();
        System.out.println(111);
    }

    public static void swap() {
        HashMapTest a = new HashMapTest();
        HashMapTest b = new HashMapTest();
        HashMapTest c = a;
        System.out.println(a == c);

        a = b;
        System.out.println(a == b);

        b = c;

    }


    public static void hash(){
        System.out.println( "############################");
        String str = "name";
        int h = str.hashCode();
        System.out.println(h);
        int rightMove = h >>> 16;  //65536
        System.out.println(rightMove);
        int hash = h ^ rightMove;
        System.out.println(hash);
    }

    public static void add(){
        System.out.println( "############################");
        for (int i = 0; i < 10; ++i) {
            System.out.println("doing !");
            System.out.println(i);
        }

        System.out.println( "############################");
        for (int i = 0; i < 10; i++) {
            System.out.println("doing !");
            System.out.println(i);
        }
    }

    public static void tableSizeFor(){
        int cap = 16;
        System.out.println("##############################");
        int n = cap - 1;
        System.out.println(Integer.toBinaryString(n));
        System.out.println(n>>>1);
        System.out.println(8|7);
        n |= n >>> 1;
        n |= n >>> 2;
        n |= n >>> 4;
        n |= n >>> 8;
        n |= n >>> 16;
//        int result = (n < 0) ? 1 : (n >= MAXIMUM_CAPACITY) ? MAXIMUM_CAPACITY : n + 1;
        int result = 0;
        System.out.println(result);
    }


    public static void keySet(){
        System.out.println("###################");
        Map map = new HashMap();
        map.put("a","a");
        map.put("d","a");
        map.put("s","a");
        map.put("c","a");
        map.put("b","a");
        System.out.println(map.keySet());
        Map integerSet = new HashMap();
        integerSet.put("5","");
        integerSet.put("2","");
        integerSet.put("0","");
        integerSet.put("1","");
        integerSet.put("3","");
        integerSet.put("1","");
        integerSet.put("4","");
        System.out.println(integerSet.keySet());
    }
}
